package server

import (
	"awesomeTask/application/client"
	"awesomeTask/application/models"
	"awesomeTask/application/variable"
	"awesomeTask/commom/MessageHelper"
	"awesomeTask/commom/parse"
	"awesomeTask/config"
	"awesomeTask/system/event"
	"awesomeTask/system/safe"
)

type ServerEventHandler interface {
	pushTask()
	RecordNodeInfo(message interface{})
}
type ServerEventHandlerObj struct {
}

func (handler ServerEventHandlerObj) RecordNodeInfo(message interface{}) {
	info := message.(map[string]interface{})
	safe.Lock()
	node := client.Nodes[info["ipAddress"].(string)]
	node.CpuNum = info["cpu"].(float64)
	if node.IsInit == false { //初次报告节点信息
		info := make(map[string]interface{})
		info["uniqueId"] = config.GetConfigByKey("uniqueId")
		MessageHelper.SendEvent(variable.ON_POST_SERVER_UNIQUE_ID, info, node.Connetion)
		MessageHelper.SendEvent(variable.ON_SERVER_SEND_STATUS, client.GetNodeStaus(), node.Connetion)
	}
	node.IsInit = true
	node.UsedCpu = info["cpuUsed"].(float64)
	node.RunningTask = parseHelper.Float64ParseInt(info["currentRun"].(float64))
	node.Memory = info["totalMemory"].(float64)
	node.FreeMemory = info["freeMemory"].(float64)
	node.UniqueId = info["uniqueId"].(string)
	node.BindIp = info["bindIp"].(string)
	node.Location = info["location"]
	node.BindPort = int(info["bindPort"].(float64))
	client.Nodes[info["ipAddress"].(string)] = node
	safe.Unlock()
	nodeInfo := map[string]interface{}{
		info["ipAddress"].(string): node,
	}
	event.Dispatch("RecordNodeInfo", nodeInfo)

}
func (handler ServerEventHandlerObj) OnClientTaskFinish(message interface{}) {
	info := message.(map[string]interface{})
	models.NextTick(info["taskId"].(float64))
}
